from enum import Enum
import numpy as np
class ARKitBlendShapeLocation(Enum):
    BrowDownLeft = 1
    BrowDownRight = 2
    BrowInnerUp = 3
    BrowOuterUpLeft = 4
    BrowOuterUpRight = 5
    CheekPuff = 6
    CheekSquintLeft = 7
    CheekSquintRight = 8
    EyeBlinkLeft = 9
    EyeBlinkRight = 10
    EyeLookDownLeft = 11
    EyeLookDownRight = 12
    EyeLookInLeft = 13
    EyeLookInRight = 14
    EyeLookOutLeft = 15
    EyeLookOutRight = 16
    EyeLookUpLeft = 17
    EyeLookUpRight = 18
    EyeSquintLeft = 19
    EyeSquintRight = 20
    EyeWideLeft = 21
    EyeWideRight = 22
    JawForward = 23
    JawLeft = 24
    JawOpen = 25
    JawRight = 26
    MouthClose = 27 
    MouthDimpleLeft = 28
    MouthDimpleRight = 29
    MouthFrownLeft = 30
    MouthFrownRight = 31
    MouthFunnel = 32
    MouthLeft = 33
    MouthLowerDownLeft = 34
    MouthLowerDownRight = 35
    MouthPressLeft = 36
    MouthPressRight = 37
    MouthPucker = 38
    MouthRight = 39
    MouthRollLower = 40
    MouthRollUpper = 41
    MouthShrugLower = 42
    MouthShrugUpper = 43
    MouthSmileLeft = 44
    MouthSmileRight = 45
    MouthStretchLeft = 46
    MouthStretchRight = 47
    MouthUpperUpLeft = 48
    MouthUpperUpRight = 49
    NoseSneerLeft = 50
    NoseSneerRight = 51
    TongueOut = 52

blendshape37_legacy = ['mouth_stretch_u', 'tooth_d_d', 'tongue_d', 'tooth_r', 'eye_blink1_l', 'jaw_thrust_c', 'mouth_dimple_l', 'mouth_funnel_dl', 'mouth_funnel_dr', 'mouth_funnel_ul', 'mouth_funnel_ur', 'mouth_lipCornerDepressFix_l', 'mouth_lipCornerDepressFix_r', 'mouth_lipCornerPull_l', 'mouth_lipCornerPull_r', 'mouth_lipStretchOpen_l', 'mouth_lipStretchOpen_r', 'mouth_lowerLipDepress_l', 'mouth_lowerLipDepress_r', 'brow_raise_r', 'mouth_lowerLipProtrude_c', 'mouth_pressFix_c', 'mouth_press_l', 'mouth_pucker_l', 'mouth_pucker_r', 'mouth_screamFix_c', 'mouth_sideways_l', 'mouth_suck_dr', 'mouth_suck_ul', 'mouth_suck_ur', 'mouth_upperLipRaise_l', 'mouth_upperLipRaise_r', 'nose_wrinkle_l', 'nose_wrinkle_r', 'cheek_puff_r', 'tooth_l', 'eye_lookDown2_l']

blendshape116 = ['brow_lower_l',
'tongue_Scale__X',
'tongue_Scale_Y',
'tongue_Scale__Y',
'tongue_Scale_Z',
'tongue_Scale__Z',
'nose_out_l',
'nose_out_r',
'tongue_u',
'tongue_u_u',
'brow_raise_d',
'cheek_suck_r',
'mouth_stretch_u',
'tongue_u_d',
'tooth_d_d',
'tongue_d',
'tooth_r',
'tooth_d_u',
'cheek_UP',
'eye_blink1_l',
'eye_blink1_r',
'eye_blink2_l',
'eye_blink2_r',
'eye_lidTight_l',
'eye_lidTight_r',
'eye_shutTight_l',
'eye_shutTight_r',
'brow_lower_r',
'eye_upperLidRaise_l',
'eye_upperLidRaise_r',
'eye_downLidRaise_l',
'eye_downLidRaise_r',
'jaw_sideways_l',
'jaw_sideways_r',
'jaw_thrust_c',
'mouth_chew_c',
'mouth_chinRaise_d',
'mouth_chinRaise_u',
'brow_raise_c',
'mouth_dimple_l',
'mouth_dimple_r',
'mouth_funnel_dl',
'mouth_funnel_dr',
'mouth_funnel_ul',
'mouth_funnel_ur',
'mouth_lipCornerDepressFix_l',
'mouth_lipCornerDepressFix_r',
'mouth_lipCornerDepress_l',
'mouth_lipCornerDepress_r',
'brow_raise_l',
'mouth_lipCornerPullOpen_l',
'mouth_lipCornerPullOpen_r',
'mouth_lipCornerPull_l',
'mouth_lipCornerPull_r',
'mouth_lipStretchOpen_l',
'mouth_lipStretchOpen_r',
'mouth_lipStretch_l',
'mouth_lipStretch_r',
'mouth_lowerLipDepress_l',
'mouth_lowerLipDepress_r',
'brow_raise_r',
'mouth_lowerLipProtrude_c',
'mouth_oh_c',
'mouth_oo_c',
'mouth_pressFix_c',
'mouth_press_l',
'mouth_press_r',
'mouth_pucker_l',
'mouth_pucker_r',
'mouth_screamFix_c',
'mouth_sideways_l',
'cheek_puff_l',
'mouth_sideways_r',
'mouth_stretch_c',
'mouth_suck_dl',
'mouth_suck_dr',
'mouth_suck_ul',
'mouth_suck_ur',
'mouth_upperLipRaise_l',
'mouth_upperLipRaise_r',
'nose_wrinkle_l',
'nose_wrinkle_r',
'cheek_puff_r',
'tooth_l',
'eye_lookDown1_l',
'eye_lookDown2_l',
'eye_lookLeft_l',
'eye_lookRight_l',
'eye_lookUp_l',
'eye_lookDown1_r',
'eye_lookDown2_r',
'eye_lookLeft_r',
'eye_lookRight_r',
'cheek_raise_l',
'eye_lookUp_r',
'tongue_Rot_1X',
'tongue_Rot__1X',
'tongue_Rot_2X',
'tongue_Rot__2X',
'tongue_Rot_3X',
'tongue_Rot__3X',
'tongue_Rot_1Y',
'tongue_Rot__1Y',
'tongue_Rot_2Y',
'cheek_raise_r',
'tongue_Rot__2Y',
'tongue_Rot_3Y',
'tongue_Rot__3Y',
'tongue_Rot_1Z',
'tongue_Rot__1Z',
'tongue_Rot_2Z',
'tongue_Rot__2Z',
'tongue_Rot_3Z',
'tongue_Rot__3Z',
'tongue_Scale_X',
'cheek_suck_l']

var_bs_index = [10, 13, 14, 15, 18, 33, 38, 40, 41, 42, 43, 44, 45, 51, 52, 53, 54, 57, 58, 59, 60, 63, 64, 66, 67, 68,
                    69, 74, 75, 76, 77, 78, 79, 80, 81, 82, 84]
blendshape37 = []
for i in var_bs_index:
    blendshape37.append(blendshape116[i])

name2blendshap37map = {}
for i,name in enumerate(blendshape37):
    name2blendshap37map[name] = i
def  getbs37Value(bs37,name):    
    if name in name2blendshap37map:
        return bs37[name2blendshap37map[name]]
    return 0.0

def maxbs(bs,names):
    values = []
    for n in names:
        if n in name2blendshap37map:
            values.append(bs[name2blendshap37map[n]])
    return max(values)

def ConvertBs37ToARKit51(bs37):
    bs51 = np.zeros((51))
    bs51[int(ARKitBlendShapeLocation.JawForward.value)] = getbs37Value(bs37, 'jaw_thrust_c')
    bs51[int(ARKitBlendShapeLocation.JawLeft.value)] = getbs37Value(bs37, 'jaw_sideways_l')
    bs51[int(ARKitBlendShapeLocation.JawRight.value)] = getbs37Value(bs37, 'jaw_sideways_r')
    bs51[int(ARKitBlendShapeLocation.JawOpen.value)] = getbs37Value(bs37, 'mouth_stretch_c')
    bs51[int(ARKitBlendShapeLocation.MouthClose.value)] = getbs37Value(bs37, 'mouth_chew_c')
    bs51[int(ARKitBlendShapeLocation.MouthFunnel.value)] = maxbs(bs37,['mouth_funnel_dl','mouth_funnel_dr','mouth_funnel_ul','mouth_funnel_ur'])
    bs51[int(ARKitBlendShapeLocation.MouthPucker.value)] = maxbs(bs37,['mouth_pucker_l','mouth_pucker_r'])
    bs51[int(ARKitBlendShapeLocation.MouthLeft.value)] = getbs37Value(bs37, 'mouth_sideways_l')
    bs51[int(ARKitBlendShapeLocation.MouthRight.value)] = getbs37Value(bs37, 'mouth_sideways_r')
    bs51[int(ARKitBlendShapeLocation.MouthSmileLeft.value)] = getbs37Value(bs37, 'mouth_lipCornerPull_l')
    bs51[int(ARKitBlendShapeLocation.MouthSmileRight.value)] = getbs37Value(bs37, 'mouth_lipCornerPull_r')
    bs51[int(ARKitBlendShapeLocation.MouthFrownLeft.value)] =  maxbs(bs37,['mouth_lipCornerDepress_l','mouth_lipCornerDepressFix_l'])
    bs51[int(ARKitBlendShapeLocation.MouthFrownRight.value)] =  maxbs(bs37,['mouth_lipCornerDepress_r','mouth_lipCornerDepressFix_r'])
    bs51[int(ARKitBlendShapeLocation.MouthDimpleLeft.value)] = getbs37Value(bs37, 'mouth_dimple_l')
    bs51[int(ARKitBlendShapeLocation.MouthDimpleRight.value)] = getbs37Value(bs37, 'mouth_dimple_r')
    bs51[int(ARKitBlendShapeLocation.MouthStretchLeft.value)] = getbs37Value(bs37, 'mouth_lipStretch_l')
    bs51[int(ARKitBlendShapeLocation.MouthStretchRight.value)] = getbs37Value(bs37, 'mouth_lipStretch_r')
    bs51[int(ARKitBlendShapeLocation.MouthRollLower.value)] =  maxbs(bs37,['mouth_suck_dl','mouth_suck_dr'])
    bs51[int(ARKitBlendShapeLocation.MouthRollUpper.value)] =  maxbs(bs37,['mouth_suck_ul','mouth_suck_ur'])
    bs51[int(ARKitBlendShapeLocation.MouthShrugLower.value)] = getbs37Value(bs37, 'mouth_chinRaise_d')
    bs51[int(ARKitBlendShapeLocation.MouthShrugUpper.value)] = getbs37Value(bs37, 'mouth_chinRaise_u')
    bs51[int(ARKitBlendShapeLocation.MouthPressLeft.value)] = getbs37Value(bs37, 'mouth_press_l')
    bs51[int(ARKitBlendShapeLocation.MouthPressRight.value)] = getbs37Value(bs37, 'mouth_press_r')
    bs51[int(ARKitBlendShapeLocation.MouthLowerDownLeft.value)] = getbs37Value(bs37, 'mouth_lowerLipDepress_l')
    bs51[int(ARKitBlendShapeLocation.MouthLowerDownRight.value)] = getbs37Value(bs37, 'mouth_lowerLipDepress_r')
    bs51[int(ARKitBlendShapeLocation.MouthUpperUpLeft.value)] = getbs37Value(bs37, 'mouth_upperLipRaise_l')
    bs51[int(ARKitBlendShapeLocation.MouthUpperUpRight.value)] = getbs37Value(bs37, 'mouth_upperLipRaise_r')
    return list(bs51)


# ARKitBlendShapeLocation.BrowDownLeft = "browDown_Left"
# ARKitBlendShapeLocation.BrowDownRight = "browDown_Right"
# ARKitBlendShapeLocation.BrowInnerUp = "browInnerUp"
# ARKitBlendShapeLocation.BrowOuterUpLeft = "browOuterUp_L"
# ARKitBlendShapeLocation.BrowOuterUpRight = "browOuterUp_R"
# ARKitBlendShapeLocation.CheekPuff = "cheekPuff"
# ARKitBlendShapeLocation.CheekSquintLeft = "cheekSquint_L"
# ARKitBlendShapeLocation.CheekSquintRight = "cheekSquint_R"
# ARKitBlendShapeLocation.EyeBlinkLeft = "eyeBlinking_Left"
# ARKitBlendShapeLocation.EyeBlinkRight = "eyeBlinking_Right"
# ARKitBlendShapeLocation.EyeLookDownLeft = "eyeLookDown_L"
# ARKitBlendShapeLocation.EyeLookDownRight = "eyeLookDown_R"
# ARKitBlendShapeLocation.EyeLookInLeft = "eyeLookIn_L"
# ARKitBlendShapeLocation.EyeLookInRight = "eyeLookIn_R"
# ARKitBlendShapeLocation.EyeLookOutLeft = "eyeLookOut_L"
# ARKitBlendShapeLocation.EyeLookOutRight = "eyeLookOut_R"
# ARKitBlendShapeLocation.EyeLookUpLeft = "eyeLookUp_L"
# ARKitBlendShapeLocation.EyeLookUpRight = "eyeLookUp_R"
# ARKitBlendShapeLocation.EyeSquintLeft = "eyeSquint_L"
# ARKitBlendShapeLocation.EyeSquintRight = "eyeSquint_R"
# ARKitBlendShapeLocation.EyeWideLeft = "eyeWide_Left"
# ARKitBlendShapeLocation.EyeWideRight = "eyeWide_Right"
# ARKitBlendShapeLocation.JawForward = "jawForward"
# ARKitBlendShapeLocation.JawLeft = "jawLeft"
# ARKitBlendShapeLocation.JawOpen = "jawOpen"
# ARKitBlendShapeLocation.JawRight = "jawRight"
# ARKitBlendShapeLocation.MouthClose = "mouthClosed"
# ARKitBlendShapeLocation.MouthDimpleLeft = "mouthDimple_L"
# ARKitBlendShapeLocation.MouthDimpleRight = "mouthDimple_R"
# ARKitBlendShapeLocation.MouthFrownLeft = "mouthFrown_L"
# ARKitBlendShapeLocation.MouthFrownRight = "mouthFrown_R"
# ARKitBlendShapeLocation.MouthFunnel = "mouthFunnel"
# ARKitBlendShapeLocation.MouthLeft = "mouthLeft"
# ARKitBlendShapeLocation.MouthLowerDownLeft = "mouthLowerDown_L"
# ARKitBlendShapeLocation.MouthLowerDownRight = "mouthLowerDown_R"
# ARKitBlendShapeLocation.MouthPressLeft = "mouthPress_L"
# ARKitBlendShapeLocation.MouthPressRight = "mouthPress_R"
# ARKitBlendShapeLocation.MouthPucker = "mouthPucker"
# ARKitBlendShapeLocation.MouthRight = "mouthRight"
# ARKitBlendShapeLocation.MouthRollLower = "mouthRollLower"
# ARKitBlendShapeLocation.MouthRollUpper = "mouthRollUpper"
# ARKitBlendShapeLocation.MouthShrugLower = "mouthShrugLower"
# ARKitBlendShapeLocation.MouthShrugUpper = "mouthShrugUpper"
# ARKitBlendShapeLocation.MouthSmileLeft = "mouthSmile_L"
# ARKitBlendShapeLocation.MouthSmileRight = "mouthSmile_R"
# ARKitBlendShapeLocation.MouthStretchLeft = "mouthStretch_L"
# ARKitBlendShapeLocation.MouthStretchRight = "mouthStretch_R"
# ARKitBlendShapeLocation.MouthUpperUpLeft = "mouthUpperUp_L"
# ARKitBlendShapeLocation.MouthUpperUpRight = "mouthUpperUp_R"
# ARKitBlendShapeLocation.NoseSneerLeft = "noseSneer_L"
# ARKitBlendShapeLocation.NoseSneerRight = "noseSneer_R"
# ARKitBlendShapeLocation.TongueOut = "tongue_tongueOut"